package com.yzy.fly.service.util;

import com.google.common.collect.Lists;
import com.yzy.fly.entity.core.FlyClass;
import com.yzy.fly.entity.role.FlyAttribute;
import com.yzy.fly.entity.role.FlyModel;
import com.yzy.fly.entity.role.FlyTable;
import com.yzy.fly.entity.role.FlyTableColumn;
import com.yzy.fly.type.FieldType;
import com.yzy.fly.type.PermissionModifierType;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.List;

public class FlyModelJavaBeanFileUtil {

    //TODO
    public static String javaFile(FlyModel model) {
        FlyClass flyClass = FlyModelConfigUtil.ConfigConvert.modelToClassMain(model);
//
        List<String> importClassList = Lists.newArrayList();
        StringBuffer classFile = new StringBuffer("");


        StringBuffer packageBuffer = new StringBuffer("");
        StringBuffer importBuffer = new StringBuffer("");
        StringBuffer classBuffer = new StringBuffer("");

        String name = (flyClass.getPackageName()+"."+flyClass.getClassName()).replace(".","/");
        //"com/yzy/bi/entity/core/Test"
        String superName = flyClass.getSuperName();
        if ("java.lang.Object".equals(superName)) {
            superName = "";
        }

        if (StringUtils.isNotBlank(superName)) {
            superName = superName.replace(".","/");
            classBuffer.append(" extends").append(superName);
        }

        String[] interfaces = new String[]{"java/io/Serializable" };

        //package
        packageBuffer.append("package ").append(model.getPackageName()).append(";").append("\n");
        //import
//        classFile.append("import ").append(model.getPackage());
//        classFile.append("\n");
        //class
        classBuffer.append("@Data").append("\n");

        FlyTable table = model.getTable();
        List<FlyTableColumn> columns = null;
        if (table != null) {
            String tableName = table.getName();
            classBuffer.append("@TableName(\""+tableName+"\")").append("\n");
            columns = table.getColumns();
        }

        classBuffer.append("public class ").append(model.getClassName());
//
//        //superClass
//
//        //interface
//
//
        classBuffer.append(" {");
        classBuffer.append("\n");
//        //field
        List<FlyAttribute> attrs = model.getAttrs();

        if (CollectionUtils.isNotEmpty(attrs)) {
            for (int i = 0; i < attrs.size(); i++) {
                FlyAttribute flyAttribute =  attrs.get(i);
                String fieldName = flyAttribute.getFieldName();
                String columnName = flyAttribute.getColumnName();
                if (StringUtils.isNotBlank(columnName)) {
                    classBuffer.append("     ").append("@TableField(\""+columnName+"\")").append("\n");
                }
                classBuffer.append("     ");
                classBuffer.append(PermissionModifierType.value(flyAttribute.getAccess()).getType()).append(" ");
                FieldType javaFileType = FieldType.type(flyAttribute.getType());
                classBuffer.append(javaFileType.getType());
                classBuffer.append(" ").append(fieldName).append(";");
                classBuffer.append("\n");

                String javaClass = javaFileType.getValue();

                if (StringUtils.isNotBlank(javaClass)) {
                    if(!javaClass.startsWith("java.lang")&& !javaClass.equals("I")){
                        importClassList.add(javaClass);
                    }
                }
            }
        }

        List<FlyModel> subModels = model.getChildrens();

        if (CollectionUtils.isNotEmpty(subModels)) {
            for (int i = 0; i < subModels.size(); i++) {
                FlyModel flyModel =  subModels.get(i);

                classBuffer.append("     ").append("@TableField(exist=false)").append("\n");
                classBuffer.append("     ");
                //PermissionModifierType.value(flyModel.getAccess()).getType()
                classBuffer.append("private").append(" ");
                String className = flyModel.getClassName();
                classBuffer.append(className);
                //TODO 20220509
                classBuffer.append(" ").append(flyModel.getClassName()).append(";");
                classBuffer.append("\n");
                String javaClass = flyModel.getPackageName();

                if (StringUtils.isNotBlank(javaClass)) {
                    if(!javaClass.startsWith("java.lang")&& !javaClass.equals("I")){
                        importClassList.add(javaClass+"."+className);
                    }
                }
            }
        }

        classBuffer.append("\n");
        classBuffer.append("}");
        classBuffer.append("\n");

        String classString = classBuffer.toString();

        if (classString.contains("@TableName")) {
           importClassList.add("com.baomidou.mybatisplus.annotation.TableName");
        }
        if (classString.contains("@TableField")) {
            importClassList.add("com.baomidou.mybatisplus.annotation.TableField");
        }

        importClassList.add("lombok.Data");

        for (int i = 0; i < importClassList.size(); i++) {
            String javaImportClass =  importClassList.get(i);
            importBuffer.append("import ").append(javaImportClass).append(";").append("\n");
        }

        classFile.append(packageBuffer.toString()).append("\n");

        String importString = importBuffer.toString();
        if (StringUtils.isNotBlank(importString)) {
            classFile.append(importString).append("\n");
        }


        classFile.append(classBuffer);

        return classFile.toString();
    }


}